package org.rugby.online.impl;

import java.util.ArrayList;
import java.util.List;

import org.rugby.online.core.economy.RboEconomy;
import org.rugby.online.core.economy.RboTransferDescription;

public class DefaultEconomy extends DefaultObject implements RboEconomy {

	/**
	 * Serial
	 */
	private static final long serialVersionUID = 3556511663152565965L;

	/**
	 * Transfer descriptions
	 */
	private List<RboTransferDescription> transferDescriptions;
	
	/**
	 * Player identifier
	 */
	private long gamerId;
	
	/**
	 * Constructor
	 */
	public DefaultEconomy(long gid) {
		super();
		transferDescriptions = new ArrayList<RboTransferDescription>(0);
		gamerId = gid;
		this.setName("ECONOMY_" + String.valueOf(gamerId));
		// TODO : GAMER SHOULD CHECK THAT ECONOMY IDS ARE CONSISTENT
	}
	
	@Override
	public List<RboTransferDescription> getTransferDescription() {
		return transferDescriptions;
	}

	@Override
	public int getCurrentBalance() {
		int balance = 0;
		for (RboTransferDescription transfer : transferDescriptions) {
			if (transfer.getSourceId() == this.getGamerId()
					&& transfer.getTransferType().toString().startsWith("OUTCOME")) {
				balance -= transfer.getAmount();
			} else if (transfer.getTargetId() == this.getGamerId()
					&& transfer.getTransferType().toString().startsWith("INCOME")) {
				balance += transfer.getAmount();
			}
		}
		return balance;
	}

	@Override
	public int getTotalIncomeForSeason(int season) {
		int totalIncome = 0;
		for (RboTransferDescription transfer : transferDescriptions) {
			if (transfer.getSourceId() != this.getGamerId() 
					&& transfer.getSeason() == season
					&& transfer.getTransferType().toString().startsWith("INCOME")) {
				totalIncome += transfer.getAmount();
			}
		}
		return totalIncome;
	}

	@Override
	public int getTotalOutcomeForSeason(int season) {
		int totalOutcome = 0;
		for (RboTransferDescription transfer : transferDescriptions) {
			if (transfer.getSourceId() == this.getGamerId() 
					&& transfer.getSeason() == season
					&& transfer.getTransferType().toString().startsWith("OUTCOME")) {
				totalOutcome += transfer.getAmount();
			}
		}
		return totalOutcome;
	}

	@Override
	public long getGamerId() {
		return gamerId;
	}

}
